What is JSX in React?

Introducing JSX

JSX is a funny tag syntax. It is neither a string nor HTML. In this article, i am starting from the very basics and walk through everything you need to know about JSX. If you’ve ever wanted to learn React in Hindi, You can visit our Youtube Channel React Playlists. Here in this article, i will show you 10 points to give you depth knowledge about JSX. 

Introduction of JSX

  1. JSX stands for javascript XML.
  2. It is a syntax extension to Javascript.
  3. JSX is a preprocessor step that adds XML syntax to Javascript.
  4. JSX produces React “element”. It is possible to create elements without JSX but JSX makes React a lot more elegant.
  5. It is recommended to use JSX with React to describe what the UI should look like.
  6. It also allows React to show more useful error and warning messages.

7. JSX is easier to read and write. Babel transforms these expressions into an actual Javascript Code.

For Example:-

Consider the following JSX Element:

The JavaScript delivered to the browser will look like this:

In the above example, you can see the JSX code looks like HTML but it is not an HTML. Lets clear above example with syntax declaration:

JSX Syntax:

Javascript Syntax:

8. JavaScript Expression in JSX

we can put any valid JavaScript expression inside the curly braces in JSX. You can pass any JavaScript expression as children, by enclosing it with {}. In React we are allowed to use normal JavaScript expressions with JSX.

Syntax:- {expression}

Example:-

9. Specifying Attributes with JSX

you may use quotes to specify string literals as attributes.

Syntax:

Example:

You may also use curly braces to embed a JavaScript expression in an attribute.

Note:-

  • Since JSX is closer to JavaScript than to HTML, React DOM uses camelCase property naming convention instead of HTML attribute names.
  • Don’t put quotes around curly braces when embedding a JavaScript expression in an attribute. You should either use quotes (For String value) or braces (For expression) but not both in the same attribute.

10. Object Representation of JSX

Babel compiles JSX down to React.createElement() calls.

Now that you understand JSX, you can start writing our first React components. 

Reference:

Learn React Tutorials Video in Hindi

Related posts